package jsu.yym.tiktok.server;

import jsu.yym.tiktok.pojo.User;
import jsu.yym.tiktok.pojo.Video;
import jsu.yym.tiktok.mapper.UserDataMapper;
import jsu.yym.tiktok.mapper.UserMapper;
import jsu.yym.tiktok.mapper.VideoMapper;
import jsu.yym.tiktok.service.UserDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.Map.Entry;

import javax.annotation.Resource;
import java.util.Map;
@Component
public class CollaborativeFiltering {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserDataMapper userDataMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Resource
    private UserDataService userDataService;


    // 根据用户推荐视频
    public List<Video> recommendVideos(User user) {
        // 计算用户相似度
        Map<User, Double> userSimilarity = calculateUserSimilarity(user);

        // 找到最相似的用户
        User mostSimilarUser = findMostSimilarUser(userSimilarity);

        // 获取最相似用户观看过但当前用户未观看的视频
        List<Video> recommendedVideos = getUnwatchedVideos(mostSimilarUser, user);

        return recommendedVideos;
    }

    // 计算用户相似度
    private Map<User, Double> calculateUserSimilarity(User user) {
        // 实现用户相似度计算逻辑
        Map<User, Double> map = new LinkedHashMap<>();
        List<User> users = userMapper.selectList(null);
        for (User u:users) {
            Double count = userDataService.calculateUserSimilarity(user,u);
            map.put(u,count);
        }
        return map;
    }

    // 找到最相似的用户
    private User findMostSimilarUser(Map<User, Double> userSimilarity) {
        // 实现查找最相似用户的逻辑
        User userWithMaxValue = null;
        double maxValue = Double.MIN_VALUE; // 初始设为最小可能值

        for (Entry<User, Double> entry : userSimilarity.entrySet()) {
            User user = entry.getKey();
            double value = entry.getValue();

            if (value > maxValue) {
                maxValue = value;
                userWithMaxValue = user;
            }
        }

        return userWithMaxValue;
    }

    // 获取最相似用户观看过但当前用户未观看的视频
    private List<Video> getUnwatchedVideos(User mostSimilarUser, User user) {
        // 实现获取未观看视频的逻辑
        //获取两个用户的历史观看记录
        List<Integer> userData1 = userDataMapper.getUserDataByIds(user.getUsername());
        List<Integer> userData2 = userDataMapper.getUserDataByIds(mostSimilarUser.getUsername());
        // 找到最相似用户观看过但当前用户未观看的 video_id
        List<Integer> unwatchedVideoIds = new ArrayList<>();
        for (Integer videoId : userData2) {
            if (!userData1.contains(videoId)) {
                unwatchedVideoIds.add(videoId);
            }
        }
        if (unwatchedVideoIds.isEmpty()) {
            return null;
        }
        System.out.println("aaaaa");
        for (Integer a:unwatchedVideoIds) {
            System.out.println(a);
        }
        List<Video> videos = videoMapper.getVideosByPkIds(unwatchedVideoIds);
        return videos;
    }





}

